import Vue from 'vue'

export function change(goods, buynumber){
    buynumber = parseInt(buynumber)
    var id=goods.id
    if (id == null) {
        return;
    }

    var shopingcar = Vue.prototype.cache("shopingcar")
    if (shopingcar == null) {
        shopingcar = []
    }
    var _index = -1
    for (var i in shopingcar) {
        if (id == shopingcar[i].id) {
            _index = i
        }

    }
    var newnumber=0;
    if (_index != -1) {
        newnumber = shopingcar[_index]['buynumber'] + buynumber
        shopingcar[_index]['buynumber'] = newnumber
    } else {
        newnumber = buynumber
        shopingcar.push({ id: id, buynumber: newnumber })
    }
    if (newnumber > goods['number']) {
        Vue.prototype.showloading(goods.name + "库存不足")
        return;
    }
    if (newnumber <1) {
        del(goods.id)
        return;
    }
    Vue.prototype.cache("shopingcar", shopingcar)
}

export function get() {
    return Vue.prototype.cache("shopingcar");
}
export function clear() {
    Vue.prototype.cache("shopingcar", "del");
}


export function del(id,cb) {



    var shopingcar = Vue.prototype.cache("shopingcar")
    if (shopingcar == null) {
        shopingcar = []
    }
    var newlist = []
    for (var i in shopingcar) {
        if (id != shopingcar[i].id) {
            newlist.push(shopingcar[i])
        }
    }
    Vue.prototype.cache("shopingcar", newlist);

}


export function pay(orderno,cb){

}